/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <gtest/gtest.h>
#include <internal/bus/bus_base/bus.h>
#include <internal/chip/chip_base/chip.h>
#include <memory>

namespace {

// 使用 mock_bus 来测试 bus_base 的功能
class mock_bus_for_base : public dev::bus_base {
public:
    uint32_t read(ACCESS_DATA_S& input) override {
        (void)input;
        return HAS_OK;
    }

    uint32_t write(ACCESS_DATA_S& input) override {
        (void)input;
        return HAS_OK;
    }

    uint8_t get_id() override {
        return 0;
    }

    void bus_lock() override {
    }

    void bus_unlock() override {
    }
};

class bus_base_test : public ::testing::Test {
protected:
    mock_bus_for_base m_bus;
};

// 测试构造函数和析构函数
TEST_F(bus_base_test, constructor_and_destructor) {
    mock_bus_for_base bus;
    EXPECT_EQ(bus.get_left_chip(), nullptr);
}

// 测试 set_left_chip 和 get_left_chip
TEST_F(bus_base_test, set_and_get_left_chip) {
    auto chip = std::make_shared<dev::chip_base>();
    m_bus.set_left_chip(chip);
    
    auto retrieved_chip = m_bus.get_left_chip();
    EXPECT_NE(retrieved_chip, nullptr);
    EXPECT_EQ(retrieved_chip, chip);
}

// 测试 set_left_chip 设置为 nullptr
TEST_F(bus_base_test, set_left_chip_nullptr) {
    auto chip = std::make_shared<dev::chip_base>();
    m_bus.set_left_chip(chip);
    EXPECT_NE(m_bus.get_left_chip(), nullptr);
    
    m_bus.set_left_chip(nullptr);
    EXPECT_EQ(m_bus.get_left_chip(), nullptr);
}

// 测试多次设置 left_chip
TEST_F(bus_base_test, set_left_chip_multiple_times) {
    auto chip1 = std::make_shared<dev::chip_base>();
    auto chip2 = std::make_shared<dev::chip_base>();
    
    m_bus.set_left_chip(chip1);
    EXPECT_EQ(m_bus.get_left_chip(), chip1);
    
    m_bus.set_left_chip(chip2);
    EXPECT_EQ(m_bus.get_left_chip(), chip2);
    EXPECT_NE(m_bus.get_left_chip(), chip1);
}

// 测试 get_id 默认返回值
TEST_F(bus_base_test, get_id_default) {
    EXPECT_EQ(m_bus.get_id(), 0);
}

// 测试 get_left_chip 在未设置时返回 nullptr
TEST_F(bus_base_test, get_left_chip_uninitialized) {
    EXPECT_EQ(m_bus.get_left_chip(), nullptr);
}

} // namespace

